<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\ResourceTotalAccessNumUser;
use App\Validate\ResourceTotalAccessNumUserValidate;

/**
 * 用户访问资源信息
 */
class ResourceAccessUserController extends Controller
{
    public $model = null;
    public $libraryInfoModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceTotalAccessNumUser();
        $this->validate = new ResourceTotalAccessNumUserValidate();
    }

    /**
     * 列表
     * @param token 用户token
     * @param page int 页码
     * @param limit int 分页大小
     * @param big_type_id 资源大类
     */
    public function lists()
    {
        $user_id = $this->request->user_info['id'];
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;
        $big_type_id = $this->request->big_type_id;

        $res = $this->model->getUserAccessNumResourceList($user_id, null, $big_type_id, $limit);
        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 删除用户访问记录
     * @param token 用户token
     * @param ids 访问记录id  多个逗号拼接
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $user_id = $this->request->user_info['id'];
        $res = $this->model->delAccessNumUser($this->request->ids, $user_id);
        if (!$res) {
            return $this->returnApi(202, "删除失败");
        }
        return $this->returnApi(200, "删除成功", true);
    }
}
